package com.masterclient.app.businessmodule.homemodule.fragment;

import android.graphics.Rect;
import android.os.Build;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.eoner.baselib.utils.status.StatusBarUtils;
import com.eoner.baselibrary.utils.GlideUtil;
import com.eoner.baselibrary.widget.CommonTabLayout;
import com.eoner.managerlibrary.router.AroutePath;
import com.masterclient.app.BaseFragment;
import com.masterclient.app.R;
import com.masterclient.app.businessmodule.homemodule.contract.EcItemContract;
import com.masterclient.app.businessmodule.homemodule.contract.EcItemPresenter;
import com.masterclient.app.modules.income.model.EcItemDataBean;
import com.masterclient.app.modules.order.adapter.SaleOrderViewPagerAdapter;
import com.masterclient.app.utils.CommonUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @auther hjian
 * @date 2020/9/18.
 */
@Route(path = AroutePath.EXHIBITS_CLASSIFY_FRAGMENT)
public class ExhibitsClassifyFragment extends BaseFragment<EcItemPresenter> implements EcItemContract.View {
    @BindView(R.id.view_title)
    View viewTitle;
    @BindView(R.id.sale_orders_tab)
    CommonTabLayout saleOrdersTab;
    @BindView(R.id.sale_orders_pager)
    ViewPager saleOrdersPager;
    @BindView(R.id.view_head)
    View viewHead;
    @BindView(R.id.rl_brands_selector)
    RelativeLayout rlBrandsSelector;
    @BindView(R.id.iv_arrow)
    ImageView ivArrow;
    @BindView(R.id.shadow)
    ImageView shadow;
    @BindView(R.id.iv_img)
    ImageView ivImg;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_time)
    TextView tvTime;

    private List<String> tabs = new ArrayList<>();
    private List<Fragment> fragments;
    private SaleOrderViewPagerAdapter viewPagerAdapter;
    private int position = 0;

    private NewSelectorPopWindow pop;
    private boolean popShowing;
    private EcItemDataBean.HotProductsItem hotProductsItem;

    public static ExhibitsClassifyFragment getInstance() {
        return new ExhibitsClassifyFragment();
    }

    @Override
    protected View getRootView(LayoutInflater inflater) {
        return inflater.inflate(R.layout.fragment_exhibits_classify, null);
    }

    @Override
    protected void initOnlyOnce() {
        super.initOnlyOnce();

        StatusBarUtils.immersive(getActivity());
        StatusBarUtils.setPaddingSmart(getActivity(), viewTitle);
        fragments = new ArrayList<>();
        mPresenter.getProducts(String.valueOf(1), PAGE_SIZE, "");
    }

    @OnClick({R.id.rl_arrow_right,R.id.view_head})
    void onClick(View view) {
        if (view.getId() == R.id.rl_arrow_right) {
            if (popShowing) {
                pop.dismiss();
                popShowing = false;
            } else {
                showAsDropDown(pop, viewHead);
                popShowing = true;
            }
        }else if (view.getId() == R.id.view_head) {
            if (hotProductsItem != null){
                CommonUtil.goAnyWhere(mContext,hotProductsItem.productUrl,"","","","","");
            }
        }
    }

    private void showAsDropDown(PopupWindow pw, View anchor) {
        if (Build.VERSION.SDK_INT >= 24) {
            Rect visibleFrame = new Rect();
            anchor.getGlobalVisibleRect(visibleFrame);
            int height = anchor.getResources().getDisplayMetrics().heightPixels - visibleFrame.bottom;
            pw.setHeight(height);
        }
        pw.showAsDropDown(anchor);
    }

    @Override
    public void getProductsSuccess(EcItemDataBean dataBean) {
        if (dataBean != null) {
            //头部信息
            if (dataBean.hotProduct != null) {
                this.hotProductsItem = dataBean.hotProduct;
                tvTitle.setText(dataBean.hotProduct.name);
                tvTime.setText(dataBean.hotProduct.author);
                GlideUtil.displayImage(mContext, dataBean.hotProduct.image, ivImg, R.mipmap.defaults);
            }
            //tab+viewPager
            if (dataBean.categorys != null && dataBean.categorys.size() > 0) {
                tabs.clear();
                for (int i = 0; i < dataBean.categorys.size(); i++) {
                    tabs.add(dataBean.categorys.get(i).categoryName);
                }
                for (int i = 0; i < dataBean.categorys.size(); i++) {
                    saleOrdersTab.addTab(tabs.get(i), i == 0);
                    ECItemFragment ecItemFragment = ECItemFragment.getInstance(dataBean.categorys.get(i).categoryId);
                    ecItemFragment.setOnSuccessCallBackListener(hotProductsItem -> {
                        //刷新头部信息
                        if (hotProductsItem != null) {
                            this.hotProductsItem = hotProductsItem;
                            tvTitle.setText(hotProductsItem.name);
                            tvTime.setText(hotProductsItem.author);
                            GlideUtil.displayImage(mContext, hotProductsItem.image, ivImg, R.mipmap.defaults);
                        }
                    });
                    fragments.add(ecItemFragment);
                }

                init();
                initSelectorPop(dataBean.categorys);
            }
        }
    }

    private void init() {
        viewPagerAdapter = new SaleOrderViewPagerAdapter
                (getActivity().getSupportFragmentManager(), fragments, tabs);
        saleOrdersPager.setAdapter(viewPagerAdapter);
        saleOrdersPager.addOnPageChangeListener(new TabLayout.
                TabLayoutOnPageChangeListener(saleOrdersTab.getTabLayout()));
        saleOrdersPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
               if (pop != null){
                   pop.changeSelect(position);
               }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        saleOrdersTab.setupWithViewPager(saleOrdersPager);
        saleOrdersPager.setOffscreenPageLimit(1);
        saleOrdersPager.setCurrentItem(position);
    }

    private void initSelectorPop(List<EcItemDataBean.CategoryItem> list) {
        pop = new NewSelectorPopWindow(mContext, list);
        pop.changeSelect(0);
        pop.setOnItemClickListerner(new NewSelectorPopWindow.OnItemClickListerner() {
            @Override
            public void clickItem(int position) {
                saleOrdersTab.getTabLayout().getTabAt(position).select();
            }

            @Override
            public void onDismiss() {
                popShowing = false;
            }
        });
    }

    @Override
    public void getProductsFailed(String msg) {

    }
}

